import {AfterViewInit, Component} from "@angular/core";
import {HttpService} from "../HttpService";
import {UploadFile} from "../model/UploadFile";

declare const qq:any;
declare const $:any;


@Component({
    selector: 'my-rightContent',
    templateUrl:"./upload.component.html",
    styleUrls:["./upload.component.css"],
})
export class UploadSingleComponent extends HttpService implements AfterViewInit{


    _uploadArray:Array<UploadFile>=new Array();

    _upload:any=null;
    _message:string="";

    set Message(value:string){

        let m=(value||"").trim();
        if(m==""){
            this._message=m;
        }
        else {
            this._message=value;
            $("#warming").modal("show");
        }
    }

    get Message():string{
        return this._message;
    }


    private getSizeStr(size:number):string {
        if (size < 0) return size + "";
        if (size < 1024) return size + "B";
        let m;
        m = size / 1024.0;
        if (m < 1024) return m.toFixed(2) + "KB";
        m = m / 1024.0;
        return m.toFixed(2) + "MB";

    }


    ngAfterViewInit(): void {

        let parentClass:any=this;

        if(!this._upload) this._upload= new qq.FineUploaderBasic({
            button:document.getElementById("SelectFile"),
            autoUpload:false,
            debug:true,
            multiple:false,
            request:{
                endpoint:this.prefix+ "JSSBUploadFile"
            },


            callbacks:{
                onSubmit(id:number,name:string){

                    let uploadfile=new UploadFile();
                    uploadfile.ID=id;
                    uploadfile.FileName=name;
                    uploadfile.fileSize=parentClass._upload.getSize(id);

                    parentClass._uploadArray.push(uploadfile);

                    console.info("onSubmit:\r\n"+JSON.stringify(uploadfile));
                },
                onValidate(data:any){

                    if(data.size>=this._options.validation.sizeLimit){
                        parentClass.Message="文件太大，文件最大不能超过"+parentClass.getSizeStr(this._options.validation.sizeLimit);
                    }

                    console.info("onValidate\r\n"+JSON.stringify(data));

                },
                onUpload(id:number,name:string){
                    //开始上传

                    let uploadFile= parentClass._uploadArray.find((v:any)=>{ return v.ID===id});

                    uploadFile.initialize();
                    uploadFile.State.isStartUpload=true;

                    console.info("onUpload\r\n"+JSON.stringify(uploadFile));
                },
                onProgress(id:number,name:string,uploadedBytes:number,totalBytes:number){

                    let uploadFile= parentClass._uploadArray.find((v:any)=>{ return v.ID===id});


                    uploadFile.CurrentProgress=uploadedBytes/(totalBytes*1.0);


                    console.info("onProgress"+JSON.stringify(uploadFile)+"\r\n");
                },
                onComplete(id:number,name:string,responseJSON:any){


                    let uploadFile= parentClass._uploadArray.find((v:any)=>{ return v.ID===id});

                    uploadFile.State.isStartUpload=false;



                    if(responseJSON&&responseJSON.result!=null){
                        uploadFile.State.success=responseJSON.result;
                        uploadFile.State.error=!responseJSON.result;
                        if(!responseJSON.result) uploadFile.CurrentProgress=0;
                    }else if(responseJSON&&responseJSON.success!=null){
                        uploadFile.State.success=responseJSON.success;
                        uploadFile.State.error=!responseJSON.success;
                        if(!responseJSON.success) uploadFile.CurrentProgress=0;
                    }



                    console.info("onComplete"+JSON.stringify(uploadFile));
                    console.info("onComplete"+JSON.stringify(responseJSON));
                },
               /* onError(id:number,name:string,errorReason:any){
                    classThis._state.isStartUpload=false;
                    classThis._state.success=false;
                    classThis._state.error=true;

                    console.info("onError"+JSON.stringify(classThis._state));
                    console.info("onError"+JSON.stringify(errorReason));
                }*/
            },
            validation: {
                acceptFiles:"application/vnd.ms-excel , application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                allowedExtensions: ["xlsx","xls"],
                itemLimit:1,
                sizeLimit:1024*1024*8
                /*   itemLimit: 3,
                 sizeLimit: 51200 // 50 kB = 50 * 1024 bytes*/
            }

        });
    }

    constructor(){super()}

    removeFiles():void{
        if(!this._upload) return;
        this._upload.cancelAll();
        this._uploadArray=new Array();
        this._message="";
    }


    removeFile(id:number):void{
        if(!this._upload) return;
        this._upload.cancel(id);
        this._uploadArray=this._uploadArray.filter((v:any)=>{v.ID!==id});
        this._message="";
    }


    uploadStart():void{
        if(this._upload) this._upload.uploadStoredFiles();
    }


}
